import { PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcrypt';

const prisma = new PrismaClient();

/**
 * 创建测试数据脚本
 */
async function createTestData() {
  try {
    console.log('\n=== 开始创建测试数据 ===\n');

    // 1. 创建管理员用户
    console.log('1. 创建管理员用户...');
    const hashedPassword = await bcrypt.hash('admin123', 10);

    const admin = await prisma.user.upsert({
      where: { openid: 'test_admin_openid' },
      update: {},
      create: {
        openid: 'test_admin_openid',
        username: 'admin',
        password: hashedPassword,
        nickname: '测试管理员',
        role: 'ADMIN'
      }
    });
    console.log(`   ✅ 管理员创建成功 - ID: ${admin.id}, 用户名: ${admin.username}`);

    // 2. 创建普通用户
    console.log('\n2. 创建普通用户...');
    const user1 = await prisma.user.upsert({
      where: { openid: 'test_user1_openid' },
      update: {},
      create: {
        openid: 'test_user1_openid',
        nickname: '测试用户1',
        role: 'USER'
      }
    });
    console.log(`   ✅ 普通用户1创建成功 - ID: ${user1.id}, 昵称: ${user1.nickname}`);

    const user2 = await prisma.user.upsert({
      where: { openid: 'test_user2_openid' },
      update: {},
      create: {
        openid: 'test_user2_openid',
        nickname: '测试用户2',
        role: 'USER'
      }
    });
    console.log(`   ✅ 普通用户2创建成功 - ID: ${user2.id}, 昵称: ${user2.nickname}`);

    // 3. 创建测试地点（管理员创建，自动通过）
    console.log('\n3. 创建测试地点...');
    const location1 = await prisma.location.create({
      data: {
        name: '观畴园食堂',
        coordinateX: 100,
        coordinateY: 150,
        status: 'APPROVED',
        creatorId: admin.id
      }
    });
    console.log(`   ✅ 地点1创建成功 - ID: ${location1.id}, 名称: ${location1.name}`);

    const location2 = await prisma.location.create({
      data: {
        name: '紫荆园食堂',
        coordinateX: -80,
        coordinateY: 200,
        status: 'APPROVED',
        creatorId: admin.id
      }
    });
    console.log(`   ✅ 地点2创建成功 - ID: ${location2.id}, 名称: ${location2.name}`);

    const location3 = await prisma.location.create({
      data: {
        name: '桃李园食堂',
        coordinateX: 0,
        coordinateY: -100,
        status: 'PENDING',
        creatorId: user1.id
      }
    });
    console.log(`   ✅ 地点3创建成功 - ID: ${location3.id}, 名称: ${location3.name}, 状态: PENDING`);

    // 4. 创建评分
    console.log('\n4. 创建测试评分...');
    await prisma.rating.create({
      data: {
        locationId: location1.id,
        userId: user1.id,
        score: 4.5
      }
    });
    await prisma.rating.create({
      data: {
        locationId: location1.id,
        userId: user2.id,
        score: 4.0
      }
    });
    await prisma.rating.create({
      data: {
        locationId: location2.id,
        userId: user1.id,
        score: 3.5
      }
    });
    console.log(`   ✅ 评分创建成功`);

    // 5. 创建推荐菜品
    console.log('\n5. 创建推荐菜品...');
    await prisma.recommendedDish.create({
      data: {
        locationId: location1.id,
        userId: user1.id,
        dishName: '宫保鸡丁',
        floor: '一楼',
        price: 12.5
      }
    });
    await prisma.recommendedDish.create({
      data: {
        locationId: location1.id,
        userId: user2.id,
        dishName: '红烧肉',
        floor: '二楼',
        price: 15.0
      }
    });
    await prisma.recommendedDish.create({
      data: {
        locationId: location2.id,
        userId: user1.id,
        dishName: '麻辣香锅',
        floor: '一楼',
        price: 18.0
      }
    });
    console.log(`   ✅ 推荐菜品创建成功`);

    // 6. 创建拥堵程度
    console.log('\n6. 创建拥堵程度...');
    await prisma.congestionLevel.create({
      data: {
        locationId: location1.id,
        timePeriod: 'BREAKFAST',
        level: 2
      }
    });
    await prisma.congestionLevel.create({
      data: {
        locationId: location1.id,
        timePeriod: 'LUNCH',
        level: 5
      }
    });
    await prisma.congestionLevel.create({
      data: {
        locationId: location1.id,
        timePeriod: 'DINNER',
        level: 4
      }
    });
    await prisma.congestionLevel.create({
      data: {
        locationId: location2.id,
        timePeriod: 'LUNCH',
        level: 3
      }
    });
    console.log(`   ✅ 拥堵程度创建成功`);

    console.log('\n=== 测试数据创建完成 ===\n');
    console.log('测试账号信息:');
    console.log('  管理员账号: admin / admin123');
    console.log('  普通用户: 使用小程序登录模拟\n');

  } catch (error) {
    console.error('\n❌ 创建测试数据失败:', error);
    throw error;
  } finally {
    await prisma.$disconnect();
  }
}

// 执行脚本
createTestData()
  .then(() => {
    process.exit(0);
  })
  .catch((error) => {
    console.error('脚本执行失败:', error);
    process.exit(1);
  });
